<?php
// +----------------------------------------------------------------------
// | PHP [ JUST YOU ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2017 http://www.jyphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: FlyingPig <flyingpigfp@163.com>
// +----------------------------------------------------------------------

namespace App\Http\Controllers;

use App\Common\Tool;
use App\Exceptions\ApiException;
use App\Logic\RoomRecLogic;
use Illuminate\Support\Facades\DB;


/**
 * 创建房间 普通 | 俱乐部
 * Class Room
 * @package App\Http\Controllers
 */
class RoomRec extends Base
{

    /**
     * 创建房间
     * @return mixed
     * @throws ApiException
     */
    public function create()
    {
        DB::beginTransaction();
        try {
//            RoomRecLogic::instance()->checkRoomTicket($this->uid, $this->param['use_cards']);
//            if ($this->param['use_cards'] == 1) {
//                $board_choose = 3;
//            }elseif ($this->param['use_cards'] == 2) {
//                $board_choose = 6;
//            } else {
//                throw new ApiException('开房房卡数量错误');
//            }
            $pwd = Tool::redisRand();
            $id  = RoomRecLogic::instance()->createRoom($this->param, $this->uid, $pwd);
            if (!is_numeric($id)) {
                return $this->returnData($id);
            }
            Tool::setRedisRoomKey($id,$pwd);
            RoomRecLogic::instance()->reduce($this->uid, $this->param['use_cards'],$id);
            RoomRecLogic::instance()->action($id, $this->uid);
            DB::commit();
            return $this->returnData([
                'room_info' => [
                    'room_id' => $id,
                    'uid' => $this->uid,
                    'room_pwd' => $pwd,
                    'board' => $this->param['board_choose'],
                    'cost_ticket' => 0
                ]
            ]);
        } catch (\Exception $exception) {
            DB::rollBack();
            throw new ApiException($exception->getMessage(), $exception->getCode() == 0 ? 6 : $exception->getCode());
        }
    }


    /**
     * 解散房间
     * @return mixed
     * @throws ApiException
     */
    public function dissolution()
    {

        return $this->returnData(
            RoomRecLogic::instance()->dissolutionLogic($this->param['id'], $this->uid)
        );
    }


}